package com.study.algorithm.datastruct.graph.model;

import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

import static com.study.algorithm.utils.LamdaUtil.DistinctByKey;

/**
 * 使用set集合的原因，原目的是为了使用其集合元素不能重复的特性
 * 但是对于非基本类型，需要重写Vertex.class和Edge.class的equal方法（连带还要重写hashcode）才能达到效果
 * 但是考虑到目前能想到的应用场景很少，这个方法不能轻易修改
 * 所以暂时没用用到其优势
 */

public class Graph {
    private Set<Vertex> vertices;
    private Set<Edge> edges;

    public Graph() {
        vertices = new HashSet<>();
        edges = new HashSet<>();
    }

    public Set<Vertex> getVertices() {
        return vertices;
    }

    public Set<Edge> getEdges() {
        return edges;
    }


    public void removeRedundantVertice() {
        Set<Vertex> resultSet =
                this.vertices.stream().
                        filter(DistinctByKey(Vertex::getVertex))
                        .collect(Collectors.toSet());
        this.vertices = resultSet;
    }

    public void removeRedundantEdge() {
        Set<Edge> resultSet =
                this.edges
                        .stream()
                        .filter(
                                DistinctByKey(
                                        e -> "startvertex:" + e.getStartVetex() + ",endVertex:" + e.getEndVetex()
                                )
                        )
                        .collect(Collectors.toSet());
        this.edges = resultSet;
    }

}
